﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace Tilver.Data.DataPager
{
    /// <summary>
    /// 数据分页对象类
    /// </summary>
    public class DbPager : IPager
    {
        private IPager _pager;

        #region Constructs
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="pageSize">页大小</param>
        public DbPager(string sql, long pageSize) : this(sql, pageSize, null, DbDataFactory.Create()) { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="keyField">关键键（主键或唯一键）</param>
        public DbPager(string sql, long pageSize, string keyField) : this(sql, pageSize, keyField, DbDataFactory.Create()) { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="keyField">关键键（主键或唯一键）</param>
        /// <param name="connectionStringName">连接字符串名</param>
        public DbPager(string sql, long pageSize, string keyField, string connectionStringName) : this(sql, pageSize, keyField, DbDataFactory.Create(connectionStringName)) { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="database">数据库对象</param>
        public DbPager(string sql, long pageSize, DbDataBase database) : this(sql, pageSize, null, database) { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="keyField">关键键（主键或唯一键）</param>
        /// <param name="database">数据库对象</param>
        public DbPager(string sql, long pageSize, string keyField, DbDataBase database)
        {
            if (database.DbProviderType == DbProviderType.SQLServer)
            {
                _pager = new SqlServerPager(sql, pageSize, keyField, database);
            }
            else if (database.DbProviderType == DbProviderType.Oracle)
            {
                _pager = new OraclePager(sql, pageSize, keyField, database);
            }
            else if (database.DbProviderType == DbProviderType.MySql)
            {
                _pager = new MySqlPager(sql, pageSize, keyField, database);
            }
            else
            {
                throw new Exception("Can't support the provider!");
            }
        }
        #endregion

        public long GetRecordCount()
        {
            return _pager.GetRecordCount();
        }

        public DbDataReader GetPageData(long pageIndex)
        {
            return _pager.GetPageData(pageIndex);
        }

        public void AddParameters(IList<DbParameter> paras)
        {
            _pager.AddParameters(paras);
        }
    }
}
